# import requests

# cookies = {
#     'MUSIC_U': '003FB4700028A433217B84847F30D152E87B742F5AA996E982479B3DD6F7BADB01C0241D9DCE1F1494B2018F0C0254C075ADAB356DD4185AD609A34B682E83582464D36E033DFD270CC1DA01BD33D7D5A20438A0B152CA6FA7106E5873E4F17DB90E9AE5F2BBB695E91CB752A08D109E8AA85536D744B0DDB5BBF8B5ED6FAA6846A85E0699B1BA5BC822E80BAC8DC0BCB90CB86C3DA8B74474A74C659E6A23280D2FC4C5DA83E3DA46197B09029DAB737CC6E3D0A914002855D67A635CCFCADFB62B6E0B851491BD5D3F63C13D4BAB844B81E22C9A19137740FD287E3FF82BEF5A38B01939740CD8647A919C8B574DC188D87DA70ADEA22E2E0B5EC564D9310BC0CDB10AED41E92CE108961FA91E47BFD1FBCC98011DC64DD41E4424B26E4C93C252AEB2D9ECEC64C12AA6AD3127E961638CC1589D8C00F9595640ECE2E8DAEA285CB707C922B0087333D87E0A4B541EEABDEDE37970D419B08128AB836D08F77647FFA4E1015E947DB7EDD895AA31DCEDEA8D1E23B55EC3744E2C303488DC017FA7E415C3AF97F7EFE2A68C5DCB1F5E3C',
# }

# headers={
#     'referer':'https://music.163.com/',
#     'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
# }

# params = {
#     'csrf_token': 'deebd90ed14db91213cf4f7ef9e376b7',
# }

# data = {
#     'params': 'NlOmk1ltiKrbn9fth+DHbe9sJGI6QM/3kYXEu7ZYVPQ7BNmyB5w6V29TZ0uTm2cyv3d8USpufjKe8tU27s3sl4+5CGGUOH2ycAOYY4iGgULbmeAemwpwLkQQnfp6n9bZ+XrGVFjcvDhPL4q0NJszI9U5xrPVcpYjAKzB0Dso9SgpyGesZTcIjc9syPLNwMVBA+n9/IwgU/eOHISAmMdxUqtO8WTNqI+BzBkUfSDtYTbjsXSXTdlBy7k7p6RF73QIaDF4T7bzlyYiym5CUFSaA/8B4CUh6AH9mz57XCGj41zYZU2L95CFa+rv28ni+AGo',
#     'encSecKey': '5cb41bdeebecb75c51e35929b32063f2b7ea73dc343f86f65abd4a928aa643bbddebbeda96515a769e12f20727138b3b5375de1f0c531a6249342a8077351f5a9bbe1de544f2f5d5acca2153a60e6fa0bd0798993b89df8809644ff2998f2bd5e9aa727747439b9d4fc69ce1da5324eeaa550d270156be458db55a5ad829b988',
# }

# response = requests.post(
#     'https://music.163.com/weapi/cloudsearch/get/web',
#     params=params,
#     cookies=cookies,
#     headers=headers,
#     data=data,
# )
# print(response.text)

import requests
import re
from bs4 import BeautifulSoup
import execjs
import json
headers={
    'referer':'https://music.163.com/',
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
}

cookies = {
    'MUSIC_U': '0047871AEE16CEAD415D0FFB7CE5D7BFA53428DE257AA0FF066BD789BED042AA9E77BD6F59E8E6E3836E280304CBAFB396C5CCB4A9B76EF8FB9722B690F6C0644B7BE885969D6DBA71B7A5F43C39A6033A6D906E4A2CEBAD7DF7B84FD3B0A57B7B15F4D7E8B1A2065D48F10268E7979DB430554DEF59CD385CDD8D25C994435161F4C1754DC02430E188264029C1F5AF44B2469B00DAA997EB094AF69EC4EF1AE458C1541166FE68BCB79C486AFCD792158CC4A92E62DB85FCC9267CE31C528A94D60D52E04D7F8DD938EFC96C2FA36CB8E35603D539D7879E7DE7816E9FFBF324C9B4212DEDD9418EB5749FF580B8DD6790045939281D724030020CD17F0E02203B6207968ADDF6F0EDCD55C1CF7B7431A77B85BFE0AF1B34972AAC371CC47CDE7174D69ADE01FA4E8FAA4C4196BE73981F452DF975087ADE00BA575E3F4936F51CFE781E56C954687C0340F1961A7075563289FBD0F59751E19A31A9970512D93BA8AC35954EC994158D18E9F31A04897CDB00A498B6BA7629ABE97FC523E46F8549517ED405437D1B6CC7BEDA3F2BD02B39D4D51D4EE10848E376425F64712E',
}

params = {
    'csrf_token': '1389aa3d1643ab3815a4a79534d9242e',
}

keyword=input("请输入网易云音乐歌手或歌曲名称:")

url='https://music.163.com/weapi/cloudsearch/get/web'

with open('wyy.js', encoding='utf-8') as f:
    js_code = execjs.compile(f.read())
    i1x = {
        "hlpretag": "<span class=\"s-fc7\">",
        "hlposttag": "</span>",
        "s": keyword,
        "type": "1",
        "offset": "0",
        "total": "true",
        "limit": "5",
        "csrf_token": "deebd90ed14db91213cf4f7ef9e376b7"
    }
    r = js_code.call('GetSign',i1x)

    post_url='https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=9df19c4351bd16ecd0fa9e3a2afd49bb'

    data = {
        "params":r.get("encText"),
        "encSecKey":r.get("encSecKey")
    }

    print(data)

    response = requests.post(
    url=url,
    params=params,
    cookies=cookies,
    headers=headers,
    data=data,
)
print(response.text)
result = json.loads(response.text)
if result.get("code") == 200 and "songs" in result["result"]:
    for song in result["result"]["songs"]:
        print("-" * 50)  # 分隔线
        print(f"歌曲名称: {song['name']}")
        print(f"歌曲ID: {song['id']}")
        print(f"歌曲专辑: {song['al']['name']}")
        # 处理多歌手情况（用逗号拼接）
        singers = ",".join([ar["name"] for ar in song["ar"]])
        print(f"歌曲歌手: {singers}")
        # 时长转换为 分:秒 格式（原单位是毫秒）
        duration = song["dt"] // 1000  # 转换为秒
        print(f"歌曲时长: {duration // 60}:{duration % 60:02d}")  # 格式化如 2:35
        print(f"歌曲封面: {song['al']['picUrl']}")
        # 修复VIP判断：fee=1代表需要VIP/付费，fee=0代表免费
        # 同时结合播放权限pl判断（0=无权限，非0=有免费播放权限）
        privilege = song.get("privilege", {})
        is_vip = "是" if (privilege.get("fee") == 1 and privilege.get("pl") == 0) else "否"
        print(f"是否VIP歌曲: {is_vip}")
        print("-" * 50)  # 分隔线